package com.pboc.web;

import com.pboc.base.BaseException;
import com.pboc.base.PageModel;
import com.pboc.entity.SysWage;
import com.pboc.service.SysWageService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/wage")
public class SysWageController {
    @Autowired
    private SysWageService sysWageService;

    /**
     * 前往工资管理页面
     *
     * @return
     */
    @RequestMapping("view")
    public String view() {
        return "/wage/wage_view";
    }

    /**
     * 获取工资列表
     *
     * @param pageModel
     * @param sysPerson
     * @param rows
     * @param page
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public PageModel list(PageModel<SysWage> pageModel, SysWage sysPerson, Integer rows, Integer page) {
        pageModel.setPageSize(rows);
        pageModel.setPageNumber(page);
        pageModel.setQueryObj(sysPerson);
        sysWageService.getList(pageModel);
        return pageModel;
    }

    /**
     * 前往新增或修改工资明细页面
     *
     * @return
     */
    @RequestMapping("info")
    public String toWageInfoView(Integer id, Model model) {
        model.addAttribute("wage", sysWageService.getById(id));
        return "/wage/wage_info";
    }

    /**
     * 前往批量修改页面
     *
     * @return
     */
    @RequestMapping("batchEditView")
    public String toBatchEditView() {
        return "/wage/batchEdit_view";
    }

    /**
     * 前往同项设置页面
     *
     * @return
     */
    @RequestMapping("/sameEditView")
    public String toSameEditView() {
        return "/wage/sameEdit_view";
    }

    @RequestMapping("deptPrint")
    public String toDeptPrint(String sysDate,Integer deptId,Model model) {
        SysWage wage=new SysWage();
        wage.setSysDate(sysDate);
        wage.setDeptId(deptId);
        List<SysWage> wages = sysWageService.getList(wage);
        if(!CollectionUtils.isEmpty(wages)){
            model.addAttribute("deptName", wages.get(0).getDeptName());
            model.addAttribute("wages", wages);
        }
        return "/wage/deptPrint";
    }

    /**
     * 前往汇总查询页面
     *
     * @return
     */
    @RequestMapping("/sumView")
    public String toSumView() {
        return "/wage/sumView";
    }

    /**
     * 一键生成上月工资
     *
     * @return
     */
    @RequestMapping("/initWageForMonth")
    @ResponseBody
    public Map initWageForMonth() {
        Map map = new HashMap();
        try {
            sysWageService.initWageForMonth();
            map.put("success", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
        }
        return map;
    }
    @RequestMapping("/add")
    @ResponseBody
    public Map add(SysWage wage) {
        Map map = new HashMap();
        try {
            sysWageService.add(wage);
            map.put("success", true);
        }catch (BaseException e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("update")
    @ResponseBody
    public Map update(SysWage wage){
        Map map=new HashMap();
        try{
            sysWageService.updateWage(wage);
            map.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("/del")
    @ResponseBody
    public Map del(Integer id){
        Map map = new HashMap();
        try{
            sysWageService.del(id);
            map.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("/batchEdit")
    @ResponseBody
    public Map batchEdit(String sysDate, String detail, Integer deptId, String money){
        Map map = new HashMap();
        try{
            sysWageService.doBatchEdit(sysDate, detail, deptId, money);
            map.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }
    @RequestMapping("/sameEdit")
    @ResponseBody
    public Map sameEdit(String sysDate, String detail, Integer deptId, String money,Integer type){
        Map map = new HashMap();
        try{
            sysWageService.doSameEdit(sysDate, detail, deptId, money,type);
            map.put("success", true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("success", false);
            map.put("mes", "系统内部错误");
        }
        return map;
    }

    /**
     * 获取汇总查询列表
     * @param sysDate
     * @return
     */
    @RequestMapping("/getSumList")
    @ResponseBody
    public Map getSumList(String sysDate){
        Map map = new HashMap();
        SysWage wage=new SysWage();
        wage.setSysDate(sysDate);
        List<SysWage> sysWages = sysWageService.getSumList(wage);
        map.put("rows", sysWages);
        return map;
    }
    @RequestMapping("/printSumWage")
    public String printSumWage(String sysDate,Model model) {
        SysWage wage=new SysWage();
        wage.setSysDate(sysDate);
        model.addAttribute("wages", sysWageService.getSumList(wage));
        model.addAttribute("count", sysWageService.getSumCount(wage));
        return "/wage/printSumWage";
    }
    @RequestMapping("toChooseDept")
    public String toChooseDept(){
        return "/wage/deptPrintChoose";
    }

}
